﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Speech.Synthesis;
using DevComponents.DotNetBar;
using System.Data.SqlClient;

namespace Support_Study_English
{
    public partial class frmMain : Office2007RibbonForm
    {
        public SpeechSynthesizer _speaker;
        public frmMain()
        {
            InitializeComponent();
            _speaker = new SpeechSynthesizer();
            _speaker.Volume = 100;
        }

        protected bool checkExistingChildrenForm(string frmName)
        {
            foreach (Form frmItem in this.MdiChildren)
            {
                if (frmItem.Name.Equals(frmName))
                {
                    return true;
                }
            }
            return false;
        }

        private void btnAddVocabulary_Click(object sender, EventArgs e)
        {
            this.UseWaitCursor = true;
            if (!checkExistingChildrenForm("frmThemTu"))
            {
                frmThemTu frm = new frmThemTu();
                frm.MdiParent = this;
                frm._speaker = _speaker;
                frm.Show();
                this.UseWaitCursor = false;
            }
        }

        private void btnReviewVocabulary_Click(object sender, EventArgs e)
        {
            if (!checkExistingChildrenForm("frmReviewWordSetting"))
            {
                frmReviewWordSetting frm = new frmReviewWordSetting();
                frm.MdiParent = this;
                frm._speaker = _speaker;
                frm.Show();
            }
        }

        private void btnAddScriptText_Click(object sender, EventArgs e)
        {
            
        }

        private void bntListeningReview_Click(object sender, EventArgs e)
        {

        }

        private void btnViewSetting_Click(object sender, EventArgs e)
        {
            frmViewSetting frm = new frmViewSetting();
            frm._speaker = _speaker;
            frm.MdiParent = this;
            frm.Show();
        }

        private void btnImport_Click(object sender, EventArgs e)
        {

        }

        private void btnExportSetting_Click(object sender, EventArgs e)
        {
            frmExportSetting frm = new frmExportSetting();
            frm._speaker = _speaker;
            frm.MdiParent = this;
            frm.Show();
        }

        protected void AttachData(string pathFile)
        {
            try
            {
                SqlConnection connect = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=master;Integrated Security=True");
                connect.Open();
                try
                {
                    SqlCommand command = connect.CreateCommand();
                    command.CommandType = CommandType.Text;
                    string commandText = @"if not exists(select * from sys.databases where name = 'VonTuVungCuaTui')    
	                                            CREATE DATABASE VonTuVungCuaTui 
		                                            ON (FILENAME = '" + pathFile + @"')
		                                             FOR ATTACH;";
                    command.CommandText = commandText;

                    command.ExecuteNonQuery();
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    connect.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            string pathFile = Application.StartupPath + @"\data\VonTuVungCuaTui.mdf";
            AttachData(pathFile);
        }
    }
}
